package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.Discounts;
import chuishop.yiqimaicai.service.DiscountsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DiscountsController {

    @Autowired
    private DiscountsService discountsService;

    //查询所有商品
    @RequestMapping("findAllDiscounts")
    @ResponseBody
    public PageInfo<Discounts> findAllDiscounts(Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }

        if(pageSize==null){
            pageSize = 5;
        }

        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Discounts> list = discountsService.findAllDiscounts();
        PageInfo<Discounts> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    //根据输入查询活动
    @RequestMapping("selectDiscountsByName")
    @ResponseBody
    public PageInfo<Discounts> selectDiscountsByName(Integer pageNo, Integer pageSize, String selectInput){
        if(pageNo==null){
            pageNo=1;
        }

        if(pageSize==null){
            pageSize = 5;
        }

        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Discounts> list = discountsService.selectDiscountsByName(selectInput);
        PageInfo<Discounts> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    //添加活动
    @RequestMapping("addDiscounts")
    @ResponseBody
    public Map<Object, Object> addDiscounts(@RequestBody Discounts discounts){

        Map<Object, Object> result = new HashMap<>();

        boolean flag = discountsService.addDiscounts(discounts);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    //更新活动
    @RequestMapping("updateDiscounts")
    @ResponseBody
    public Map<Object, Object> updateDiscounts(@RequestBody Discounts discounts){

        Map<Object, Object> result = new HashMap<>();

        boolean flag = discountsService.updateDiscounts(discounts);
        if(flag){
            result.put("status", "200");
            result.put("msg","更新成功");
        }else {

            result.put("status", "500");
            result.put("msg", "更新失败");
        }
        return result;
    }

    //删除活动
    @RequestMapping("deleteDiscounts")
    @ResponseBody
    public Map<Object, Object> deleteDiscounts(String ids){

        System.out.println(ids);

        String idstr[] = ids.split(",");
//        for (String str: idstr){
//            System.out.println(str);
//        }

        Map<Object, Object> result = new HashMap<>();

        boolean flag = discountsService.deleteDiscounts(idstr);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

}
